//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.yang.newcustomcuilian.listeners;

import java.util.HashMap;
import java.util.List;
import com.yang.newcustomcuilian.NewCustomCuiLian;
import com.yang.newcustomcuilian.newapi.NewAPI;
import org.bukkit.Material;
import org.bukkit.entity.Damageable;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.player.PlayerExpChangeEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.projectiles.ProjectileSource;

public class MainListener implements Listener {
    HashMap<Integer, Double> ShEntityIdMap = new HashMap();
    HashMap<Integer, Double> XxEntityIdMap = new HashMap();

    public MainListener() {
    }

    @EventHandler(
        priority = EventPriority.LOWEST
    )
    public void EntityDamageEvent(EntityDamageEvent e) {
        if (e.getEntity() instanceof LivingEntity && e.getCause() != DamageCause.ENTITY_ATTACK) {
            double damage = e.getDamage();
            LivingEntity le = (LivingEntity)e.getEntity();
            double damage2 = damage - NewAPI.getDefense(NewAPI.addAll(NewAPI.getItemInHand(le), le.getEquipment().getHelmet(), le.getEquipment().getChestplate(), le.getEquipment().getLeggings(), le.getEquipment().getBoots(), NewAPI.getItemInOffHand(le)));
            if (damage2 < 0.0D) {
                damage2 = 0.0D;
            }

            e.setDamage(damage2);
        }

    }

    @EventHandler(
        priority = EventPriority.LOWEST
    )
    public void EntityDamageByEntityEvent(EntityDamageByEntityEvent e) {
        Entity Damager = e.getDamager();
        double damage = e.getDamage();
        if ((Damager instanceof Damageable || Damager instanceof Projectile) && e.getEntity() instanceof Damageable) {
            if (!(Damager instanceof Damageable)) {
                Projectile psw = (Projectile)Damager;
                ProjectileSource ps = psw.getShooter();
                if (ps instanceof LivingEntity && e.getEntity() instanceof LivingEntity) {
                    LivingEntity p = (LivingEntity)ps;
                    LivingEntity entity = (LivingEntity)e.getEntity();
                    List<ItemStack> list2 = NewAPI.addAll(NewAPI.getItemInHand(entity), entity.getEquipment().getHelmet(), entity.getEquipment().getChestplate(), entity.getEquipment().getLeggings(), entity.getEquipment().getBoots(), NewAPI.getItemInOffHand(p));
                    if (NewAPI.getReboundDamage(list2) * damage / 100.0D > 0.0D && this.canPVP(p, entity)) {
                        p.damage(NewAPI.getReboundDamage(list2) * damage / 100.0D);
                    }

                    if (this.XxEntityIdMap.get(psw.getEntityId()) != null && this.ShEntityIdMap.get(psw.getEntityId()) != null) {
                        damage = damage + (Double)this.XxEntityIdMap.get(psw.getEntityId()) * damage / 100.0D + (Double)this.ShEntityIdMap.get(psw.getEntityId());
                        p.setHealth(Math.min((Double)this.XxEntityIdMap.get(psw.getEntityId()) * damage / 100.0D + p.getHealth(), NewAPI.getMaxHealth(p)));
                        //判断原本血量是否20以上，如果是20血量以上就不需要再减血量了
//                        p.sendMessage("进入了1");
//                        p.setHealth(30D);
                        if (e.getDamager() instanceof Player) {
                            Player attacker = (Player) e.getDamager();

                            // 获取攻击者手持的物品
                            ItemStack weapon = attacker.getInventory().getItemInMainHand();

                            // 现在你可以使用 weapon 这个 ItemStack 对象，获取关于武器的信息
                            ItemMeta itemMeta = weapon.getItemMeta();
                            itemMeta.getLore().forEach(item ->{
                                if (item.contains("淬炼")){
                                    p.setHealth(30D);
                                }
                            });
                            // 其他操作...
                        }
                    }

                    damage -= NewAPI.getDefense(list2);
                }
            } else if (e.getEntity() instanceof LivingEntity && Damager instanceof LivingEntity) {
                LivingEntity Damagerr = (LivingEntity)Damager;
                LivingEntity entity2 = (LivingEntity)e.getEntity();
                List<ItemStack> list3 = NewAPI.addAll(NewAPI.getItemInHand(Damagerr), Damagerr.getEquipment().getHelmet(), Damagerr.getEquipment().getChestplate(), Damagerr.getEquipment().getLeggings(), Damagerr.getEquipment().getBoots(), NewAPI.getItemInOffHand(Damagerr));
                List<ItemStack> list4 = NewAPI.addAll(NewAPI.getItemInHand(entity2), entity2.getEquipment().getHelmet(), entity2.getEquipment().getChestplate(), entity2.getEquipment().getLeggings(), entity2.getEquipment().getBoots(), NewAPI.getItemInOffHand(Damagerr));
                double damage2 = damage + NewAPI.getBloodSuck(list3) * damage / 100.0D + NewAPI.getDamage(list3);
//                Damagerr.setHealth(Math.min(Damagerr.getHealth() + NewAPI.getBloodSuck(list3) * damage2 / 100.0D, NewAPI.getMaxHealth(Damagerr)));

//                Damagerr.sendMessage("进入了2");
                // 获取攻击者
                if (e.getDamager() instanceof Player) {
                    Player attacker = (Player) e.getDamager();

                    // 获取攻击者手持的物品
                    ItemStack weapon = attacker.getInventory().getItemInMainHand();

                    // 现在你可以使用 weapon 这个 ItemStack 对象，获取关于武器的信息
                    ItemMeta itemMeta = weapon.getItemMeta();
                    itemMeta.getLore().forEach(item ->{
                        if (item.contains("淬炼")){
                            Damagerr.setHealth(30D);
                        }
                    });
                    // 其他操作...
                }
                if (NewAPI.getReboundDamage(list4) * damage2 / 100.0D > 0.0D && this.canPVP(Damagerr, entity2)) {
                    Damagerr.damage(NewAPI.getReboundDamage(list4) * damage2 / 100.0D);
                }

                damage = damage2 - NewAPI.getDefense(list4);
            }
        }

        if (damage < 0.0D) {
            damage = 0.0D;
        }

        e.setDamage(damage);
    }

    @EventHandler(
        priority = EventPriority.LOWEST
    )
    public void ProjectileLaunchEvent(ProjectileLaunchEvent e) {
        Projectile psw = e.getEntity();
        LivingEntity le = (LivingEntity)psw.getShooter();
        this.ShEntityIdMap.put(psw.getEntityId(), NewAPI.getDamage(NewAPI.addAll(NewAPI.getItemInHand(le), le.getEquipment().getHelmet(), le.getEquipment().getChestplate(), le.getEquipment().getLeggings(), le.getEquipment().getBoots(), NewAPI.getItemInOffHand(le))));
        this.XxEntityIdMap.put(psw.getEntityId(), NewAPI.getBloodSuck(NewAPI.addAll(NewAPI.getItemInHand(le), le.getEquipment().getHelmet(), le.getEquipment().getChestplate(), le.getEquipment().getLeggings(), le.getEquipment().getBoots(), NewAPI.getItemInOffHand(le))));
    }

    @EventHandler(
        priority = EventPriority.LOWEST
    )
    public void PlayerMoveEvent(PlayerMoveEvent e) {
        Player p = e.getPlayer();
        int level = NewAPI.getJump(NewAPI.addAll(NewAPI.getItemInHand(p), p.getEquipment().getHelmet(), p.getEquipment().getChestplate(), p.getEquipment().getLeggings(), p.getEquipment().getBoots(), NewAPI.getItemInOffHand(p)));
        if (level != 0) {
            p.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 60, level));
        }

    }

    @EventHandler(
        priority = EventPriority.LOWEST
    )
    public void PlayerExpChangeEvent(PlayerExpChangeEvent e) {
        int value = e.getAmount();
        Player p = e.getPlayer();
        Double i = NewAPI.getExperience(NewAPI.addAll(NewAPI.getItemInHand(p), p.getInventory().getHelmet(), p.getInventory().getChestplate(), p.getInventory().getLeggings(), p.getInventory().getBoots(), NewAPI.getItemInOffHand(p)));
        if (i != 0.0D) {
            e.setAmount((int)((double)value * (i + 100.0D) / 100.0D));
        }

    }

    @EventHandler(
        priority = EventPriority.LOWEST
    )
    public void PlayerQuitEvent(PlayerQuitEvent e) {
        Player p = e.getPlayer();
        if (NewCustomCuiLian.playerSuitEffectList.containsKey(p.getUniqueId())) {
            NewAPI.setMaxHealth(p, (Double)NewCustomCuiLian.playerSuitEffectHealthList.get(p.getUniqueId()));
            NewCustomCuiLian.playerSuitEffectHealthList.remove(p.getUniqueId());
            NewCustomCuiLian.playerSuitEffectList.remove(p.getUniqueId());
        }

    }

    public boolean canPVP(LivingEntity a, LivingEntity b) {
        return true;
    }
}
